package com.zt.bus.assistant.controller;

import com.zt.bus.assistant.model.RouteStat;
import com.zt.bus.assistant.service.RouteStatService;
import com.zt.servlet.ReqBean;
import com.zt.servlet.RequestAttribute;
import com.zt.servlet.Response;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/routeStat")
public class RouteStatController {

    @Autowired
    private RouteStatService routeStatService;

    @RequestMapping(value = "/query")
    @ResponseBody

    public JSONObject query(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(routeStatService.getTable(reqBean));
    }

    @RequestMapping(value = "/queryByRoute")
    @ResponseBody

    public JSONObject queryByRoute(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(routeStatService.getByRoute(reqBean));
    }

    @RequestMapping(value = "/add")
    @ResponseBody
    
    public JSONObject add(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.responseSuccess(routeStatService.add(getRouteStat(reqBean)));
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    
    public JSONObject update(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        routeStatService.update(getRouteStat(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    
    public JSONObject delete(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        routeStatService.delete(reqBean.getInfo("routeId"));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/stops")
    @ResponseBody
    public JSONObject getStops(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(routeStatService.getStops());
    }

    @RequestMapping(value = "/stopsNearBy")
    @ResponseBody
    public JSONObject getStopsNearBy(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(routeStatService.getStopsNearBy(reqBean));
    }

    /**
    * 从表单获取对象
    *
    * @param reqBean
    * @return
    */
    private RouteStat getRouteStat(ReqBean reqBean) {
        RouteStat routeStat = new RouteStat();
        routeStat.setRouteId(reqBean.getInfo("routeId"));
        routeStat.setUserId(reqBean.getInfo("userId"));
        routeStat.setDepartTime(reqBean.getInfo("departTime"));
        routeStat.setIsWorkingDepart(reqBean.getInfo("isWorkingDepart"));
        routeStat.setIsHolidayDepart(reqBean.getInfo("isHolidayDepart"));
        routeStat.setStartStopId(reqBean.getInfo("startStopId"));
        routeStat.setStartStopName(reqBean.getInfo("startStopName"));
        routeStat.setStartStopLng(reqBean.getInfo("startStopLng"));
        routeStat.setStartStopLat(reqBean.getInfo("startStopLat"));
        routeStat.setEndStopId(reqBean.getInfo("endStopId"));
        routeStat.setEndStopName(reqBean.getInfo("endStopName"));
        routeStat.setEndStopLng(reqBean.getInfo("endStopLng"));
        routeStat.setEndStopLat(reqBean.getInfo("endStopLat"));
        routeStat.setRemark(reqBean.getInfo("remark"));
        routeStat.setDepartType(reqBean.getInfo("departType"));
        return routeStat;
    }

}